package com.demo.uid.listener;

import com.demo.uid.config.MachineIdConfig;
import com.demo.uid.service.SnowFlakeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 配置变更监听器
 */
@Slf4j
@Component
public class ConfigPropertiesListener implements ApplicationListener<RefreshScopeRefreshedEvent> {

    @Resource
    private MachineIdConfig machineIdConfig;

    @Resource
    private SnowFlakeService snowFlakeService;

    /**
     * 配置变更后重新初始化各分组snowflake对象
     * @param event
     */
    @Override
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
        snowFlakeService.init(machineIdConfig.getMachineId());
    }
}
